package com.clickpaas.governmentcloud.service.policymatchextservice.impl;

import com.bizcloud.ipaas.tb4093760b8224c4fbcff37d3fa266753.d20210127152100.model.*;
import com.clickpaas.governmentcloud.common.ConstantExt;
import com.clickpaas.governmentcloud.common.ConstantSuggest;
import com.clickpaas.governmentcloud.common.EnumCompanyType;
import com.clickpaas.governmentcloud.common.ServerResponse;
import com.clickpaas.governmentcloud.entity.policyEntity.PolicyEntity;
import com.clickpaas.governmentcloud.service.policymatchextservice.MatchExtService;
import com.clickpaas.governmentcloud.utils.policycondcalutils.PolicyCondCal;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @company <上海爱湃斯科技有限公司>
 * @email <zhihui.peng@clickpaas.com>
 * @author huiye
 * @decription: 中小企业发展专项资金（中小企业创新）
 * @date 2020/12/16 10:19 下午
 */
@Slf4j
@Service
public class SpefundSMEDevelopServiceImpl implements MatchExtService {
    @Resource
    private PolicyCondCal policyCondCal;
    /**
     * 跟政策是否匹配
     *
     * @param policy
     * @return
     */
    @Override
    public ServerResponse<?> isMatchSuccess(PolicyEntity policy) {
        /**
         * 建议
         */
        StringBuffer suggest = new StringBuffer();
        /**
         * 返回信息
         */
        ServerResponse<?> serverResponse = ServerResponse.createError(ConstantExt._ERROR_MESSAGE);
        /**
         * 评分
         */
        Integer score = ConstantExt._ZERO;
        /**
         * 是否快速匹配：Y->true,N->false
         */
        boolean isQuickMatch = ConstantExt.QUICK_MATCH.equals(policy.getType());
        /**
         *  必要条件判断
         */
        HashMap hashMap = necessCondi(policy);
        /**
         * 必要条件对快速匹配数据做处理（精确匹配、快速匹配）
         */
        boolean ifNecess=Boolean.parseBoolean(hashMap.get(ConstantExt._NECESS).toString());
        if(!ifNecess && isQuickMatch){
            return serverResponse;
        }
        /**
         * 累加必要条件评分
         */
        score = score + Integer.parseInt(hashMap.get(ConstantExt._SCORE).toString());
        suggest.append(hashMap.get(ConstantExt._SUGGEST).toString());

        HashMap suggAndScore = getScore(policy);
        /**
         * 累加评分
         */
        score = score + Integer.parseInt(suggAndScore.get(ConstantExt._SCORE).toString());
        /**
         * 评分为0，快速匹配不展示数据
         */
        if(score <= 0 && isQuickMatch){
            return serverResponse;
        }
        /**
         * 添加建议
         */
        suggest.append(suggAndScore.get(ConstantExt._SUGGEST).toString());
        /*if (suggest.length() == 0 && score >= 90) {
            suggest.append("建议申报");
        }*/
        /**
         * 快速匹配不添加建议
         */
        if (isQuickMatch){
            suggest = new StringBuffer();
        }
        /**
         * 必要条件不满足得0分
         */
        if(!ifNecess){
            score = 0;
        }
        return insertPolicyData(policy,score,suggest,serverResponse);
    }

    /**
     * 计算企业评分
     *
     * @param policy
     * @return
     */
    @Override
    public HashMap getScore(PolicyEntity policy) {
        HashMap<String, Object> returnValue = new HashMap<>();
        StringBuffer suggest = new StringBuffer();
        /**
         * 分值
         */
        Integer score = ConstantExt._ZERO;
        /**
         * 成立满5年
         */
        if(policyCondCal.ifCreateMuchYear(policy,5*365)){
            score += 5;
        }else {
            suggest.append(ConstantSuggest._DEVELOP_ESTABLISHED);
        }
        /**
         * 研发投入强度
         * 因高研发投入处于微利或未盈利阶段(默认给5分)
         */
        score += 5;
        /**
         * 是否中小企业：
         */
        boolean flagSmallMedium = false;
        if(policyCondCal.ifMediumOrMiniComp(policy)){
            score += 5;
        }else {
            flagSmallMedium = true;
        }
        /**
         * 行业领域
         * 集成电路、生物医药、人工智能、新能源汽车、区块链等重点领域
         */
        List<String> industrys = policy.getIndustrys();
        if(null != industrys
                && policyCondCal.listIntersection(industryList(),industrys)){
            score += 5;
        }else {
            if (!flagSmallMedium){
                suggest.append(ConstantSuggest._DEVELOP_ENTERPRISE);
            }
        }
        /**
         * 自主知识产权
         * ≥10	40分
         * ≥5	30分
         * ≥1	20分
         */
        Integer intellCount = policyCondCal.IntellPropertyFirst(policy)+policyCondCal.IntellPropertySecond(policy);
        if (intellCount>=10){
            score += 40;
        }else if (intellCount>=5){
            score += 30;
        }else if(intellCount>=1){
            score += 20;
        }else {
            suggest.append(ConstantSuggest._DEVELOP_INTELLECTUAL);
        }
        /**
         * 去年研发费占比
         */
        if (policyCondCal.RDPerCostRateLastYear(policy)>=20){
            score += 40;
        }else {
            suggest.append(ConstantSuggest._DEVELOP_RD_EXPENSES);
        }
        returnValue.put(ConstantExt._SCORE,score);
        returnValue.put(ConstantExt._SUGGEST,suggest);
        return returnValue;
    }

    /**
     * 写入匹配结果
     *
     * @param policy
     * @return
     */
    @Override
    public ServerResponse insertPolicyData(PolicyEntity policy,Integer score,StringBuffer suggest,ServerResponse serverResponse) {
        /**
         * 查询当前政策信息
         */
        TPoliciesDTO policyInfo = policyCondCal.getPolicyInfo(ConstantExt._SPECIAL_FUNDS_SME_DEVELOP);

        if (null == policyInfo) {
            log.info("政策：【{}】查询不到",ConstantExt._SPECIAL_FUNDS_SME_DEVELOP);
            return serverResponse;
        }
        policyCondCal.insertCommonPolicyData(policy, policyInfo, score, suggest);
        return ServerResponse.createSuccess(ConstantExt._SUCCESS_MESSAGE);
    }

    /**
     * 必要条件判断
     *
     * @param policy
     * @return
     */
    @Override
    public HashMap necessCondi(PolicyEntity policy) {
        HashMap<String, Object> map = new HashMap<>();
        Integer score = ConstantExt._ZERO;
        map.put(ConstantExt._NECESS,true);
        StringBuffer suggStr = new StringBuffer();
        map.put(ConstantExt._SCORE,score);
        map.put(ConstantExt._SUGGEST,suggStr);
        return map;
    }

    /**
     * 该政策支持的行业
     * @return
     */
    public List<String> industryList(){
        ArrayList<String> indusList = new ArrayList<>();
        indusList.add(EnumCompanyType._INTEGRATECIRCUIT.getCompanyTypeChina());
        indusList.add(EnumCompanyType._BIOLOGYNEWMEDICINE.getCompanyTypeChina());
        indusList.add(EnumCompanyType._ARTIFICIALINTELLIGENCE.getCompanyTypeChina());
        indusList.add(EnumCompanyType._NEWENERGYVEHICLES.getCompanyTypeChina());
        indusList.add(EnumCompanyType._BLOCKCHAIN.getCompanyTypeChina());
        return indusList;
    }
}
